<?php
/**
 * 管理员管理
 * @author Administrator
 */
class administrator extends admin{
   public function __construct(){
      parent::__construct();
      if((int)$_SESSION['client']['parentid']>0) $this->ajaxReturn(array('status'=>0,'info'=>'您没有该模块访问权限'));
   }

    //管理员列表
   public function index(){
		$where = ' where is_delete=0 ';
		if($_GET['keyword']!=''){
			$keyword = trim($_GET['keyword']);
			$where .= " and (name like '%$keyword%' or mobile like '%$keyword%') ";
			$url .= '?keyword='.$keyword;
		}

      $total=db()->getOne('select count(*) from x_jiaxiao_admin '.$where);
      if(isset($_GET['pagesize'])&&(int)$_GET['pagesize']>0) $pagesize=(int)$_GET['pagesize'];
      else $pagesize=C('PAGE_SIZE');
      if (isset($_GET['p']) && (int) $_GET['p'] > 0) $p = (int) $_GET['p'];
      else $p = 1;
      $limit=' limit '.($p-1)*$pagesize.','.$pagesize;
      $list=db()->getAll('select id,name,mobile,parentid,create_time,last_time,last_ip,school_id,auth_node as checked from x_jiaxiao_admin '.$where.' '.$limit);
      $page = array('total' => $total, 'pagesize' => $pagesize, 'curpage' => $p);
      $this->ajaxReturn(array('status'=>1,'data'=>$list,'page'=>$page));

   }
   
   public function auth_node(){
       $where=' where 1=1 ';
       if($_SESSION['client']['parentid']>0) $where.=' and id in('.$_SESSION['client']['auth_node'].')';
       $res=db()->getAll('select id,parentid,name from x_jiaxiao_menu '.$where.' order by parentid ASC');
       if(!$res) $res=array();
       $this->ajaxReturn(array('status'=>1,'data'=>$res));
   }

   public function update(){
      $id=(int)$_POST['id'];
      if($id<=0) $this->ajaxReturn(array('status'=>'0','info'=>'参数错误id'));
      
      $data['mobile']=$_POST['mobile'];
      if(db()->getOne('select id from x_jiaxiao_admin where mobile='.$data['mobile'].' and id<>'.$id)) {
          $this->ajaxReturn(array('status'=>0,'info'=>'该手机号已存在，请更换手机号'));
      }
      $data['name']=$_POST['name'];
      if(isset($_POST['pwd'])&&$_POST['pwd']!='')  $data['password']=md5(trim($_POST['pwd']));
      
      if($_SESSION['client']['parentid']==0) $data['auth_node']=implode(',',$_POST['checked']);
      else{
          $nodes=array_intersect($_POST['checked'],explode(',',$_SESSION['client']['auth_node']));
          $data['auth_node']=implode(',',$nodes);
      }
      if(isset($_POST['school_id'])&&!empty($_POST['school_id'])) {
          $school_ids=array_intersect($_POST['school_id'],explode(',',$_SESSION['client']['school_id']));
          $data['school_id']=implode(',',$school_ids);
      }
      $affert_num=db()->update('x_jiaxiao_admin',$data,'id='.$id.' and parentid<>0');
      if($affert_num!==false) {
         insertlogs('update','更新管理员：'.$data['name'],$data,$id);
         $this->ajaxReturn(array('status'=>1));
      }
      else $this->ajaxReturn(array('status'=>0,'info'=>'修改失败'));

   }

   public function insert(){
      $data['mobile']=$_POST['mobile'];
      if(db()->getOne('select id from x_jiaxiao_admin where mobile='.$data['mobile'])) {
          $this->ajaxReturn(array('status'=>0,'info'=>'该手机号已存在，请更换手机号'));
      }
      $data['name']=$_POST['name'];
      $data['password']=md5(trim($_POST['pwd']));
      $data['create_time']=date('Y-m-d H:i:s');
      if($_SESSION['client']['parentid']==0) $data['parentid']=$_SESSION['client']['id'];
      else $data['parentid']=$_SESSION['client']['parentid'];
      
      if($_SESSION['client']['parentid']==0) $data['auth_node']=implode(',',$_POST['checked']);
      else{
          $nodes=array_intersect($_POST['checked'],explode(',',$_SESSION['client']['auth_node']));
          $data['auth_node']=implode(',',$nodes);
      }
      if(isset($_POST['school_id'])&&!empty($_POST['school_id'])) {
          $school_ids=array_intersect($_POST['school_id'],explode(',',$_SESSION['client']['school_id']));
          $data['school_id']=implode(',',$school_ids);
      }
      else $data['school_id']=$_SESSION['client']['school_id'];
      
      $insert_id=db()->insert('x_jiaxiao_admin',$data);
      if($insert_id) {
         insertlogs('create','添加管理员：'.$data['name'],$data,$insert_id);
         $this->ajaxReturn(array('status'=>1,'info'=>'添加成功'));
      }
      else $this->ajaxReturn(array('status'=>0,'info'=>'添加失败'));
   }
    //删除管理员
    public function del(){
		$this->ajaxReturn(array('status'=>0,'info'=>'您没有删除权限'));
        $id=(int)$_POST['id'];
        $data['is_delete']=1;
        $affert_num=db()->update('x_jiaxiao_admin',$data,'id='.$id.' and parentid<>0');
        if($affert_num){
            insertlogs('update','删除账号',$data,$id);
            $this->ajaxReturn(array('status'=>1,'info'=>'删除成功'));
        }
        else $this->ajaxReturn(array('status'=>0,'info'=>'删除失败'));
    }
}
